package com.appcheck.recording.listener;

import java.lang.reflect.Field;
import java.util.HashMap;

import android.view.View;

import com.appcheck.recording.PreViewManager;
import com.appcheck.recording.utils.Logger;

public abstract class AbstractListener {
	private static Field listenerInfoField;
	private static HashMap listenersMap = new HashMap();
	private PreViewManager preViewManager;
	
	public final PreViewManager getPreViewManager() {
		return preViewManager;
	}

	public final void setPreViewManager(PreViewManager paramPreViewManager) {
		try {
			if(paramPreViewManager != null){
				this.preViewManager = (PreViewManager) paramPreViewManager.clone();
			}
		} catch (CloneNotSupportedException e) {
			this.preViewManager = null;
			e.printStackTrace();
		}
	}

	static{
		try{
			(AbstractListener.listenerInfoField = View.class.getDeclaredField("mListenerInfo")).setAccessible(true);
	    }
	    catch (SecurityException localSecurityException) {
	    	Logger.ERROR("SecurityException", localSecurityException);
	    }catch (NoSuchFieldException localNoSuchFieldException) {
	    	Logger.ERROR("NoSuchFieldException", localNoSuchFieldException);
	    }
	}
	  
	protected Object listener = null;
	  
	protected AbstractListener(View paramView, Class viewClass, Class listenerClass){
	    try{
	    	this.listener = getListenerFromView(paramView, viewClass, listenerClass);
	    	if (this.listener != null) {
	    		Logger.INFO(new StringBuilder().append(paramView.getClass().getSimpleName()).append(" listener remembered").toString());
	    	}
	    }
	    catch (Exception exception){
	    	Logger.ERROR("Couldn't get original listener from " + viewClass.getSimpleName(), exception);
	    }
	}
	  
	protected Object getListenerFromView(View paramView, Class viewClass, Class listenerClass) throws Exception {
		//e.g. viewClass = TextView.class listenerName = mOnEditorActionListener.class
		String listenerName = "m" + listenerClass.getSimpleName();
		Field listenerField = (Field)listenersMap.get(listenerName);
		
		//如果第二个类型参数传入的是View，而且mListenerInfo字段不是未定义的
		if ((viewClass.getName().equals("android.view.View")) && (listenerInfoField != null)){
			Object listenerInfo;
			//listenerInfo对应的就是mListenerInfo字段，这里的意思是从paramView中取出mListenerInfo这个字段对应的值
			if ((listenerInfo = listenerInfoField.get(paramView)) != null){
				//如果为空，说明之前没有将字段保存在HashMap中
				if (listenerField == null){
					(listenerField = listenerInfo.getClass().getDeclaredField(listenerName)).setAccessible(true);
					listenersMap.put(listenerName, listenerField);
				}
				return listenerField.get(listenerInfo);
			}
			return null;
		}
		//如果传入的类型不是View，或者mListenerInfo未定义
		if (listenerField == null){
			try {
				//从view里面直接取这个listenerName字段
				(listenerField = viewClass.getDeclaredField(listenerName)).setAccessible(true);
			} catch (NoSuchFieldException e) {
				Logger.ERROR("NoSuchFieldException found in AbstractListener", e);
			}
			listenersMap.put(listener, listenerField);
		}
		return listenerField.get(paramView);

  }
}
